﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace stsdev.Builders.DeploymentFiles
{
    public class SharePointInstallBuilder
    {
        public static void Create(string filePath)
        {
            throw new NotImplementedException("Create(string) is not allowed in the SharePointInstallBuilder object");
        }

        //<configuration>
        //  <appSettings>
        //    <add key="BannerImage" value="Default"/>
        //    <add key="LogoImage" value="Default"/>
        //    <add key="EULA" value="EULA.rtf"/>
        //    <add key="SolutionId" value="44906C46-D7C1-4a14-A7D2-87394D9FC7E3"/>
        //    <add key="SolutionFile" value="yoursolution.wsp"/>
        //    <add key="SolutionTitle" value="Enter Product Title Here"/>
        //    <add key="SolutionVersion" value="1.0.0.0"/>
        //    <add key="FeatureScope" value="Farm"/>
        //    <add key="FeatureId" value="A69D3FDA-142A-4d2c-BA6D-446CE01FF1C2"/>
        //    <add key="UpgradeDescription" value="Upgrades {SolutionTitle} on all frontend web servers in the SharePoint farm. "/>
        //    <add key="RequireDeploymentToCentralAdminWebApplication" value="true"/>
        //    <add key="RequireDeploymentToAllContentWebApplications" value="false"/>
        //    <add key="DefaultDeployToSRP" value="false"/>    
        //    <add key="RequireMoss" value="true"/>    
        //    <add key="SSPRelativeConfigLink" value="/_layouts/MyFeature/Config.aspx"/>
        //    <add key="DocumentationUrl" value="http://www.mycompany.com/MyFeatureDoc.aspx"/>	
        //  </appSettings>
        //</configuration>

        public static void Create(string filePath, string bannerImage, string logoImage, string eula, string solutionId, string solutionFile, string solutionTitle, string solutionVersion,
            FeatureScopeEnum featureScope, string featureId, string upgradeDescription, bool requireDeploymentToCentralAdminWebApplication, bool requireDeploymentToAllContentWebApplications,
            bool defaultDeployToSRP, bool requireMOSS, string sspRelativeConfigLink, string documentationUrl)
        {
            StreamWriter manifestStream = new StreamWriter(filePath);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            settings.NewLineOnAttributes = false;
            settings.NewLineHandling = NewLineHandling.None;
            XmlWriter writer = XmlWriter.Create(manifestStream, settings);
            writer.WriteStartDocument();

            writer.WriteComment("Generated by StsDev 2008 on " + DateTime.Now.ToString());
            writer.WriteStartElement("configuration");
            writer.WriteStartElement("appSettings");

            AddKey(writer, "BannerImage", bannerImage);
            AddKey(writer, "LogoImage", logoImage);
            AddKey(writer, "EULA", eula);
            AddKey(writer, "SolutionId", solutionId);
            AddKey(writer, "SolutionFile", solutionFile);
            AddKey(writer, "SolutionTitle", solutionTitle);
            AddKey(writer, "SolutionVersion", solutionVersion);
            AddKey(writer, "FeatureScope", featureScope.ToString());
            AddKey(writer, "FeatureId", featureId);
            AddKey(writer, "UpgradeDescription", upgradeDescription);
            AddKey(writer, "RequireDeploymentToCentralAdminWebApplication", requireDeploymentToCentralAdminWebApplication.ToString());
            AddKey(writer, "RequireDeploymentToAllContentWebApplications", requireDeploymentToAllContentWebApplications.ToString());
            AddKey(writer, "DefaultDeployToSRP", defaultDeployToSRP.ToString());
            AddKey(writer, "RequireMoss", requireMOSS.ToString());
            AddKey(writer, "SSPRelativeConfigLink", sspRelativeConfigLink);
            AddKey(writer, "DocumentationUrl", documentationUrl);
            
            writer.WriteEndElement(); //End appSettings
            writer.WriteEndElement(); //End configuration
        }

        private static void AddKey(XmlWriter writer, string keyName, string value)
        {
            writer.WriteStartElement("add");
            writer.WriteAttributeString("key", keyName);
            writer.WriteAttributeString("value", value);
            writer.WriteEndElement();
        }
    }
}
